import React, {Component} from 'react'
import {getAllRequests, getFundingDetailArrayByTabId,approveRequest} from '../util/interaction';
import CardList  from './cardList';
import {Button} from 'semantic-ui-react';
import RequestList from "./requestList";

class SupportFunging extends Component {
    constructor() {
        super();
        this.state = {
            supportFungings:[],
            selectedFundingDetail :'',
            requests:[]
        }

    }

// 组件初始化时自动执行的方法
    async componentDidMount() {
        try {
            let supportFungings = await getFundingDetailArrayByTabId(3);
            this.setState({
                supportFungings,
            })
        } catch (e) {
            console.log(e);
        }

    }

    //获取所有的request展示
    getRequests = async () => {
        let {0:fundingAddress} = this.state.selectedFundingDetail;
        try {
            let requests = await getAllRequests(fundingAddress);
            this.setState({requests});
        } catch (e) {
            console.log(e);
        }
    };
    onApproveClickFunc= async (index) => {
        try {
            console.log('click index :', index);
            let {0:fundingAddress} = this.state.selectedFundingDetail
            await approveRequest(fundingAddress, index);
            window.location.reload(true);
        } catch (e) {
            console.log(e)
        }
    }

    render() {
        let {supportFungings,selectedFundingDetail,requests} = this.state;
        let {7:supportersCount} = selectedFundingDetail;
        return (
            <div className="App">
                <CardList details={supportFungings} onItemClick={(detail) => {
                    this.setState({selectedFundingDetail: detail})
                }}/>

                {
                    selectedFundingDetail && (
                        <div>
                            <Button onClick={this.getRequests}>申请详情</Button>
                            <RequestList requests={requests}
                                         supportersCount={supportersCount}
                                         onApproveClick={this.onApproveClickFunc}
                                         tabKey={3}
                            />
                        </div>
                    )

                }
            </div>
        );
    }
}



export default SupportFunging;